//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "MTLCurrency.h"
using namespace Cephei::QL::Currencies;
#include <gen/QL/Math/Rounding.h>
#include <gen/QL/Currency.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Currencies::CMTLCurrency::CMTLCurrency () : CCurrency(CMTLCurrency::typeid)
{
    try
    {
#ifdef HANDLE
        _phMTLCurrency = NULL;
#endif
        _ppMTLCurrency = new boost::shared_ptr<QuantLib::MTLCurrency> (new QuantLib::MTLCurrency ( ));
        SetCurrency (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppMTLCurrency));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Currencies::CMTLCurrency::CMTLCurrency (boost::shared_ptr<QuantLib::MTLCurrency>& childNative, Object^ owner) : CCurrency(CMTLCurrency::typeid)
{
#ifdef HANDLE
	_phMTLCurrency = NULL;
#endif
	_ppMTLCurrency = &childNative;
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppMTLCurrency));
}
Cephei::QL::Currencies::CMTLCurrency::CMTLCurrency (QuantLib::MTLCurrency& childNative, Object^ owner) : CCurrency(CMTLCurrency::typeid)
{
#ifdef HANDLE
	_phMTLCurrency = NULL;
#endif
	_ppMTLCurrency = new boost::shared_ptr<QuantLib::MTLCurrency> (&childNative);
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppMTLCurrency));
    _MTLCurrencyOwner = owner;
    _CurrencyOwner = owner;
}

Cephei::QL::Currencies::CMTLCurrency::CMTLCurrency (CMTLCurrency^ copy) : CCurrency(CMTLCurrency::typeid)
{
#ifdef HANDLE
	_phMTLCurrency = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMTLCurrency = new boost::shared_ptr<QuantLib::MTLCurrency> (copy->GetShared());
        _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppMTLCurrency));
    }
}
Cephei::QL::Currencies::CMTLCurrency::CMTLCurrency (System::Type^ t) : CCurrency(CMTLCurrency::typeid)
{
#ifdef HANDLE
	_phMTLCurrency = NULL;
#endif
	if (!t->IsSubclassOf(CMTLCurrency::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Currencies::CMTLCurrency::CMTLCurrency (QuantLib::Handle<QuantLib::MTLCurrency>& childNative, Object^ owner)  : CCurrency(CMTLCurrency::typeid)
{
	_phMTLCurrency = &childNative;
	_ppMTLCurrency = &static_cast<boost::shared_ptr<QuantLib::MTLCurrency>>(childNative.currentLink());
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppMTLCurrency));
    _MTLCurrencyOwner = owner;
}
Cephei::QL::Currencies::CMTLCurrency::CMTLCurrency (QuantLib::Handle<QuantLib::MTLCurrency> childNative)  : CCurrency(CMTLCurrency::typeid)
{
	_phMTLCurrency = &childNative;
	_ppMTLCurrency = &static_cast<boost::shared_ptr<QuantLib::MTLCurrency>>(childNative.currentLink());
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppMTLCurrency));
}
#endif
#ifdef STRUCT
Cephei::QL::Currencies::CMTLCurrency::CMTLCurrency (QuantLib::MTLCurrency childNative)  : CCurrency(CMTLCurrency::typeid)
{
#ifdef HANDLE
	_phMTLCurrency = NULL;
#endif
	_ppMTLCurrency = new boost::shared_ptr<QuantLib::MTLCurrency> (new QuantLib::MTLCurrency (childNative));
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppMTLCurrency));
}
#endif

Cephei::QL::Currencies::CMTLCurrency::~CMTLCurrency ()
{
    if (_ppMTLCurrency != NULL)
    {
	    delete _ppMTLCurrency;
        _ppMTLCurrency = NULL;
    }
}
Cephei::QL::Currencies::CMTLCurrency::!CMTLCurrency ()
{
    if (_ppMTLCurrency != NULL)
    {
	    delete _ppMTLCurrency;
    }
}
QuantLib::MTLCurrency& Cephei::QL::Currencies::CMTLCurrency::GetReference ()
{
    if (_ppMTLCurrency == NULL) throw gcnew NativeNullException ();
	return **_ppMTLCurrency;
}
boost::shared_ptr<QuantLib::MTLCurrency>& Cephei::QL::Currencies::CMTLCurrency::GetShared ()
{
    if (_ppMTLCurrency == NULL) throw gcnew NativeNullException ();
	return *_ppMTLCurrency;
}
QuantLib::MTLCurrency* Cephei::QL::Currencies::CMTLCurrency::GetPointer ()
{
    if (_ppMTLCurrency == NULL) throw gcnew NativeNullException ();
	return &**_ppMTLCurrency;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MTLCurrency>& Cephei::QL::Currencies::CMTLCurrency::GetHandle ()
{
	if (_phMTLCurrency == NULL)
	{
		_phMTLCurrency = new Handle<QuantLib::MTLCurrency> (*_ppMTLCurrency);
	}
	return *_phMTLCurrency;
}
#endif
bool Cephei::QL::Currencies::CMTLCurrency::HasNative () 
{
	return (_ppMTLCurrency != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Currencies::IMTLCurrency^ Cephei::QL::Currencies::CMTLCurrency_Factory::Create ()
{
    return gcnew CMTLCurrency ();
}
